package com.bytedance.scenedemo.extreme_case;

import com.bytedance.scene.Scene;
import com.bytedance.scene.animation.AnimationInfo;
import com.bytedance.scene.animation.NavigationAnimatorExecutor;
import com.bytedance.scene.animation.TransitionUtils;
import com.bytedance.scene.group.GroupScene;
import com.bytedance.scene.interfaces.PushOptions;
import com.bytedance.scene.ktx.SceneExtensions;
import com.bytedance.scenedemo.ResourceTable;
import com.bytedance.scenedemo.benchmark.BenchmarkSamples;
import com.bytedance.scenedemo.lifecycle.EmptyScene100;
import com.bytedance.scenedemo.navigation.push_pop.PushPopBasicUsageDemoScene;
import com.bytedance.scenedemo.utility.ColorUtil;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ScrollView;
import ohos.agp.components.Text;

public class Case1Scene extends GroupScene {

    @Override
    public ComponentContainer onCreateView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {
        return (ComponentContainer) inflater.parse(ResourceTable.Layout_case_basic_layout,container,false);
    }

    @Override
    public void onViewCreated(Component layout, Intent intent) {
        super.onViewCreated(layout, intent);
        Text textHint= (Text) layout.findComponentById(ResourceTable.Id_text_case0scene_hint);
        ScrollView svMain = (ScrollView) layout.findComponentById(ResourceTable.Id_sv_push_pop_basic_usage_main);
        textHint.setText(Case1Scene.class.getName());
        int value=0;
        if(getArguments()!=null){
            value= (int) getArguments().getParam("value");
        }
        svMain.setBackground(ColorUtil.getMaterialColor(value));

        Button btnPushAndPop1 = (Button) layout.findComponentById(ResourceTable.Id_case_push_pop_btn_1);
        btnPushAndPop1.setText("Push 100次");
        btnPushAndPop1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                for (int i=0;i<=99;i++) {
                    PushOptions options = new PushOptions.Builder()
                            .setAnimation(new Case1Scene.HideTransitionAnimatorExecutor())
                            .build();
                    IntentParams intentParams = new IntentParams();
                    intentParams.setParam("value",i);
                    SceneExtensions.requireNavigationScene(Case1Scene.this).push(EmptyScene100.class,intentParams,options);
                }
            }
        });
        Button btnPushAndMany2 = (Button) layout.findComponentById(ResourceTable.Id_case_push_pop_btn_2);
        Button btnPushAndMany3 = (Button) layout.findComponentById(ResourceTable.Id_case_push_pop_btn_3);
        btnPushAndMany2.setText("");
        btnPushAndMany3.setText("");
    }

    @Override
    public void onActivityCreated(Intent intent) {
        super.onActivityCreated(intent);
    }

    public class HideTransitionAnimatorExecutor extends NavigationAnimatorExecutor {
        @Override
        public boolean isSupport(Class<? extends Scene> from, Class<? extends Scene> to) {
            return true;
        }

        @Override
        protected boolean disableConfigAnimationDuration() {
            return true;
        }

        @Override
        protected Animator onPushAnimator(final AnimationInfo fromInfo, final AnimationInfo toInfo) {
            final Component fromView = fromInfo.mSceneView;
            final Component toView = toInfo.mSceneView;

            AnimatorProperty fromAlphaAnimator =new AnimatorProperty();
            fromAlphaAnimator.setTarget(fromView)
                    .alphaFrom(1.0f).alpha(1.0f)
                    .setDuration(120 * 20);

            AnimatorProperty toAlphaAnimator=new AnimatorProperty();
            toAlphaAnimator.setTarget(toView)
                    .alphaFrom(0.0f).alpha(1.0f)
                    .setDuration(120 * 20);

            AnimatorProperty toTranslateAnimator = new AnimatorProperty();
            toTranslateAnimator.setTarget(toView)
                    .moveFromY(0.08f * toView.getHeight()).moveToY(0f)
                    .setDuration(200 * 20);

            return TransitionUtils.mergeAnimators(fromAlphaAnimator,toAlphaAnimator, toTranslateAnimator);
        }


        @Override
        protected Animator onPopAnimator(final AnimationInfo fromInfo, final AnimationInfo toInfo) {
            final Component fromView = fromInfo.mSceneView;
            final Component toView = toInfo.mSceneView;

            AnimatorProperty fromAlphaAnimator=new AnimatorProperty();
            fromAlphaAnimator.setTarget(fromView)
                    .alphaFrom(1.0f).alpha(0.0f)
                    .setDuration(150 * 20).setDelay(50 * 20);

            AnimatorProperty fromTranslateAnimator=new AnimatorProperty();
            fromAlphaAnimator.setTarget(fromView)
                    .moveFromY(0f).moveToY(0.08f * toView.getHeight())
                    .setDuration(200 * 20);

            AnimatorProperty toAlphaAnimator=new AnimatorProperty();
            toAlphaAnimator.setTarget(toView)
                    .alphaFrom(0.7f).alpha(1.0f)
                    .setDuration(20 * 20);

            return TransitionUtils.mergeAnimators(fromAlphaAnimator, fromTranslateAnimator,toAlphaAnimator);
        }
    }
}
